
package com.geek.bams.client;

import com.geek.bams.entity.Account;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.SocketException;

public class LoginPanel extends JPanel {
    /**
     * LoginPanel 类中持有 ATMClient 的引用
     * 通过构造函数传入 ATMClient 实例并保存在私有成员变量 atm 中
     * LoginPanel 依赖于 ATMClient 来完成注册业务逻辑，如调用 atm.bank.register() 方法
     * 这体现了"部分-整体"的关系，LoginPanel 是整体的一部分，ATMClient 是其运行所需的组件
     * 因此这是典型的组合关系，表示 LoginPanel 组合了 ATMClient 功能。
     */
    private ATMClient atm;

    public LoginPanel(ATMClient atm) {
        this.atm = atm;
        initComponents();
    }

    /*private void confirm(ActionEvent e) {
        //直接跳转至BusinessPanel界面
        CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
        cardLayout.show(this.getParent(),"businessPanel");
    }*/
    //点击登录，跳转至businessPanel
    private void confirm(ActionEvent e) {
        // 收集登录界面数据
        String idStr = idTextField.getText();
        String password = new String(pswTextField.getText());

        // 执行Bank中登录操作
        if (idStr != null && !idStr.equals("")) {
            /*//未加入网络编程  begin
            try {
                Long id = Long.valueOf(idStr);
                Account account = atm.bank.login(id, password);
                // 登陆成功
                if (account != null) {
                    // 初始化BusinessPanel界面数据
                    atm.account = account;
                    atm.businessPanel.initBusiness();

                    // 直接跳转至BusinessPanel界面
                    CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
                    cardLayout.show(this.getParent(), "businessPanel");

                    // 清空登录界面数据
                    clearLogin();
                }
            } catch (Exception ex) {
                // 登录失败，则弹框显示失败信息，并清空密码框
                JOptionPane.showMessageDialog(null, ex.getMessage());
                pswTextField.setText("");
            }
            //未加入网络编程  end*/

            //==================加入网络编程后  begin
            //1.发送指令   写PrintWriter
            atm.pw.println("login#"+idStr+"#"+password);
            atm.pw.flush();

            //4.等待服务器发送的结果  读ObjectInputStream
            try {
                Object o = atm.ois.readObject();

                if(o instanceof Account){
                    //登录成功，跳转至业务界面，初始化业务界面中数据
                    Account acc = (Account) o;
                    if(acc!=null){
                        //JOptionPane.showMessageDialog(this , "登录成功");
                        //跳转至业务界面
                        CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
                        cardLayout.show(this.getParent(),"businessPanel");
                        //初始化业务界面中数据
                        atm.account = acc;
                        atm.businessPanel.initBusiness();
                        //清空登录界面
                        clearLogin();
                    }
                }else if(o instanceof String){
                    //登录失败，弹框异常信息，并清空密码框
                    JOptionPane.showMessageDialog(this , o);
                    pswTextField.setText("");
                }
            }  catch (SocketException ex) {
                JOptionPane.showMessageDialog(this , "服务器已关闭");
                System.exit(0);
            }  catch (IOException ex) {
                throw new RuntimeException(ex);
            }  catch (ClassNotFoundException ex) {
                throw new RuntimeException(ex);
            }
            //==================加入网络编程后  end
        } else {
            JOptionPane.showMessageDialog(null, "账户不能为空！");
        }
    }

    //清空登录页面数据
    public void clearLogin(){
        idTextField.setText("");
        pswTextField.setText("");
    }
    //点击返回，跳转至mainPanel
    private void back(ActionEvent e) {
        // TODO add your code here
        //获取当前LoginPanel的父窗口（ATMClient），的布局方式，即卡片式布局
        CardLayout cardLayout = (CardLayout)this.getParent().getLayout();
        //指定跳转至主界面
        cardLayout.show(this.getParent() , "mainPanel");
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label2 = new JLabel();
        idTextField = new JTextField();
        pswTextField = new JTextField();
        confirm = new JButton();
        back = new JButton();
        label3 = new JLabel();

        //======== this ========
        setLayout(null);

        //---- label1 ----
        label1.setText("\u7528\u6237\u8d26\u53f7\uff1a");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));
        add(label1);
        label1.setBounds(90, 150, 90, 30);

        //---- label2 ----
        label2.setText("\u7528\u6237\u5bc6\u7801\uff1a");
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));
        add(label2);
        label2.setBounds(90, 245, 90, 30);
        add(idTextField);
        idTextField.setBounds(210, 145, 215, 40);
        add(pswTextField);
        pswTextField.setBounds(210, 240, 215, 40);

        //---- confirm ----
        confirm.setText("\u786e\u8ba4");
        confirm.addActionListener(e -> confirm(e));
        add(confirm);
        confirm.setBounds(100, 360, 80, 40);

        //---- back ----
        back.setText("\u8fd4\u56de");
        back.addActionListener(e -> back(e));
        add(back);
        back.setBounds(290, 360, 80, 40);

        //---- label3 ----
        label3.setText("\u767b\u5f55\u9875\u9762");
        label3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 24));
        add(label3);
        label3.setBounds(180, 40, 100, 40);

        setPreferredSize(new Dimension(500, 650));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label2;
    private JTextField idTextField;
    private JTextField pswTextField;
    private JButton confirm;
    private JButton back;
    private JLabel label3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
